package com.partner.frame.support.util;

import java.text.*;
import org.apache.commons.lang.*;

public class DateConvertUtil
{

   public static java.util.Date parse(String dateString, String dateFormat)
   {
      return parse(dateString, dateFormat, java.util.Date.class);
   }

   @SuppressWarnings("unchecked")
   public static <T extends java.util.Date> T parse(String dateString, String dateFormat, Class<T> targetType)
   {
      long lTime = 0;
      String errorInfo = "";
      DateFormat fmtDate = null;
      java.util.Date dateResult = null;

      if(StringUtils.isEmpty(dateString)) return null;

      fmtDate = new SimpleDateFormat(dateFormat);
      try
      {
         lTime = fmtDate.parse(dateString).getTime();
         dateResult = targetType.getConstructor(long.class).newInstance(lTime);
      }
      catch(ParseException eParse)
      {
         errorInfo = "dateformat[" + dateFormat + "] datestring[" + dateString + "]";
         throw new IllegalArgumentException(errorInfo, eParse);
      }
      catch(Exception eOther)
      {
         throw new IllegalArgumentException("error_type[" + targetType.getName() + "]", eOther);
      }

      return (T) dateResult;
   }

   public static String format(java.util.Date objDate, String dateFormat)
   {
      if(objDate == null) return null;
      
      return new SimpleDateFormat(dateFormat).format(objDate);
   }
   
}
